<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>我的合同</title>
    <link rel="stylesheet" href="../../lib/layui/layui/css/layui.css">
    <script type="text/javascript" src="../../js/news/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui/lay/dest/layui.all.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../lib/layui/layui/layui.js"></script>
    <style>
        * {font-family: "Microsoft Yahei" !important}
        nav{
            height: 57px;
            -margin-bottom: -10px;

        }
        .search{
            display: inline;
            margin-bottom: 1px;
            width: 185px;
            border: 0;
            background: #fafbfc;
            height: 35px;
            margin-top: 5px;
        }
        .allBtn{
            float: left !important;
            margin-top: 11px;
            width: 65px;
            margin-left: -8px;
            border: none;
            height: 30px;
            line-height: 30px;
        }
        .layui-tab{
            margin: 0px;
        }
        nav .layui-form-select .layui-input{
            height: 35px;
        }
    </style>
</head>
<body>
<nav>
    <form class="layui-form" action="">
        <div class="layui-form-item" style="display: inline-block; margin-top: 10px;float: left">
            <div class="layui-inline">
                <label class="layui-form-label">合同标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input title">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同类型</label>
                <div class="layui-input-inline">
                    <select name="modules" id="dictName" lay-verify="required" lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同种类</label>
                <div class="layui-input-inline">
                    <select name="contractType" class="codeName" lay-verify="required" lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同金额</label>
                <div class="layui-input-inline" style="width: 140px">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input money">
                </div>
            </div>
            <span style="margin-left: -19px">~</span>
            <div class="layui-inline">
                <div class="layui-input-inline" style="width: 140px">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input moneyEnd">
                </div>
            </div>
        </div>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm allBtn classBtn">查询</button>
        <%--新建合同--%>
        <div style="float: right; margin-top: 10px;">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="new" style="float: right;margin-right: 30px;">新建合同</button>
        </div>
    </form>
</nav>
<div>
    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
        <div class="layui-tab-content">
            <div class="content"><table class="layui-hide" id="plan" lay-filter="plan"></table></div>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="examine">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit">编辑</a>
</script>
<%--</form>--%>
</body>
<script>
    $('.content').height($(window).height() - 130);
    layui.use(['form', 'layedit', 'laydate','layedit','table'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,layedit = layui.layedit
            ,table = layui.table;

        layedit.build('texPart'); //建立编辑器
        form.render();
        $('#new').click(function () {
            location.href = '/contract/new';
        });
        //合同种类
        $.ajax({
            url: '/code/getCode',
            type: 'get',
            dataType: 'json',
            data:{
                parentNo:'CONTRACT_TYPE'
            },
            success: function (res) {
                var obj = res.obj
                var str = ''
                for (var i = 0; i < obj.length; i++) {
                    str += '<option value="' + obj[i].codeNo + '">' + obj[i].codeName + '</option>'
                }
                $('.codeName').append(str);
                form.render('select');
            }
        })

        //合同类型
        $.ajax({
            url: '/contract/selectTypeAll',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var obj = res.data
                var str = ''
                for (var i = 0; i < obj.length; i++) {
                    str += '<option value="' + obj[i].typeId + '">' + obj[i].typeName + '</option>'
                }
                $('#dictName').append(str);
                form.render('select');
            }
        })
        $('.classBtn').click(function () {
            var money = ''
            var moneyEnd = ''
            var typeId = $('#dictName').val()
            var contractType = $('.codeName').val()
            if($('.money').val()==''&&$('.moneyEnd').val()!='') {
                money = '0'
                moneyEnd = $('.moneyEnd').val()
            }else if($('.money').val()!=''&&$('.moneyEnd').val()==''){
                money = $('.money').val()
                moneyEnd = '0'
            }else{
                money = $('.money').val()
                moneyEnd =$('.moneyEnd').val()
            }
            table.reload('plan',{
                where:{
                    typeId:typeId,
                    contractType:contractType,
                    money:money,
                    moneyEnd:moneyEnd,
                    title:$('.title').val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        })
        //日期
        laydate.render({
            elem: '#date'
        });
        table.render({
            elem: '#plan'
            ,url:'/contract/selectContractAll'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,where:{
                useFlag:true
            }
            ,cols: [[
                {field:'contractNo', title: '合同编号'}
                ,{field:'title', title: '合同标题'}
                ,{field:'typeName',  title: '合同类型'}
                ,{field:'targetName', title: '合同对象'}
                ,{field:'money', title: '合同金额'}
                ,{field:'userName', title: '跟进人'}
                ,{fixed: 'right',title: '操作', toolbar: '#barDemo'}
            ]]
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            ,page:true
        });
        $('.layui-icon-search').click(function () {
            var search=$('.search').val();
            if($('.search')!=''){
                setTimeout(function () {
                    table.reload('plan',{
                        url:'/contract/selectContractBy',
                        method: 'get',
                        where:{
                            pageSize:15,
                            cha:search
                        }
                    })
                },100);
            }
        });
        //监听头工具栏事件
        table.on('tool(plan)', function(obj){
            switch(obj.event){
                case 'examine':
                    location.href='/contract/details?contractId='+obj.data.contractId;
                    break;
                case 'edit':
                    if(obj.length === 0){
                        layer.msg('请选择一行');
                    } else if(obj.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        location.href='/contract/edit?contractId='+obj.data.contractId;
                    }
                    break;
            };
        });
    });

</script>

</html>
